package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultJsydgzq;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.factory.AdjustModelFactory;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyResultJsydgzqMapper;
import com.geostar.constructionland.analyse.service.BaseAdjustService;
import com.geostar.constructionland.analyse.service.analyseresult.AlyResultJsydGzqService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author chengan
 * @description 建设用地管制区分析
 */
@Slf4j
@Component
public class JsydgzqHandler implements IResultHandler{

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    private AlyResultJsydGzqService alyResultJsydGzqService;

    @Autowired
    BaseAdjustService adjustService;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        HandlerResult result = HandlerResult.createBySuccess();
        log.info("处理建设用地管制区占压分析返回数据,状态码:{}",httpResult.getCode());
        if(HttpStatus.SC_OK==httpResult.getCode()){
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(),AlyResult.class);
            log.info("body:"+httpResult.getBody());
            if(HttpStatus.SC_OK == alyResult.getStatus()){
                /**如果成功,构建主体解析**/
                List<AlyResultJsydgzq> alyResultJsydgzqs = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultJsydgzq>>(){});
                if(alyResultJsydgzqs.size()>0){
                    alyResultJsydgzqs.forEach(jsydgzq -> {
                        jsydgzq.setFxid(UUID.randomUUID().toString().replaceAll("-",""));
                        jsydgzq.setFxbs(analyseModel.getAlyId());
                    });
                    String newResult = objectMapper.writeValueAsString(alyResultJsydgzqs);
                    alyResult.getAnalysisResult().setResultRecords(newResult);
                    /**执行平差**/
                    AdjustModel adjustModel = AdjustModelFactory.getAdjustMod(analyseModel,alyResult);
                    /**平差后的结果**/
                    List<LandInfo> landInfo_adjusted = adjustService.doAjust(analyseModel,adjustModel);

                    Map<String,LandInfo> adjustMap = landInfo_adjusted.stream().collect(Collectors.toMap(LandInfo::getFxId, landInfo -> landInfo));
                    /**更新数据**/
                    alyResultJsydgzqs.forEach(jsydgzq -> jsydgzq.setZymj(adjustMap.get(jsydgzq.getFxid()).getZymj_adjusted()));

                    /**更新数据**/
                    alyResultJsydGzqService.saveBatch(alyResultJsydgzqs);
                }
            }else{
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
